package system

import (
	"errors"
	"parking-lot-go-dingding/model"
	"time"

	"gorm.io/gorm"
)

type DingtalkSystemDepartmentUserStruct struct {
	Id               int       `json:"id" gorm:"type:int(10) unsigned auto_increment; NOT NULL; primary_key"`
	CorpId           string    `json:"corp_id" gorm:"type:varchar(50); COMMENT:'组织'"`
	DepartmentId     int       `json:"department_id" gorm:"type:int(11); COMMENT:'部门id'"`
	DepartmentName   string    `json:"department_name" gorm:"type:varchar(255); COMMENT:'部门名称'"`
	Name             string    `json:"name" gorm:"type:varchar(255);  COMMENT:'用户名'"`
	Userid           string    `json:"userid" gorm:"type:varchar(255);  COMMENT:'用户id'"`
	Unionid          string    `json:"unionid" gorm:"type:varchar(255);  COMMENT:'用户在当前开发者企业帐号范围内的唯一标识'"`
	Active           int       `json:"active" gorm:"type:tinyint(1); COMMENT:'是否激活了钉钉 1是 0否'"`
	Admin            int       `json:"admin" gorm:"type:tinyint(1); COMMENT:'是否为企业的管理员 1是 0不是'"`
	Avatar           string    `json:"avatar" gorm:"type:varchar(255);  COMMENT:'头像'"`
	Boss             int       `json:"boss" gorm:"type:tinyint(1); COMMENT:'是否为企业的老板 1是 0否'"`
	DeptIdList       string    `json:"dept_id_list" gorm:"type:text; COMMENT:'所属部门id列表'"`
	DeptOrder        int       `json:"dept_order" gorm:"type:int(11);"`
	ExclusiveAccount int       `json:"exclusive_account" gorm:"type:tinyint(1); COMMENT:'是否专属帐号 1是 0否'"`
	HideMobile       int       `json:"hide_mobile" gorm:"type:tinyint(1); COMMENT:'是否号码隐藏 1是 0否'"`
	Leader           int       `json:"leader" gorm:"type:tinyint(1);  COMMENT:'是否是部门的主管 1是 0否'"`
	Title            string    `json:"title" gorm:"type:varchar(255); COMMENT:'职位'"`
	CreatedAt        time.Time `json:"created_at" gorm:"type:datetime"`
	UpdatedAt        time.Time `json:"updated_at" gorm:"type:datetime"`
}

func (c *DingtalkSystemDepartmentUserStruct) TableName() string {
	return "dingtalk_system_department_user"
}

func (c *DingtalkSystemDepartmentUserStruct) Create(data DingtalkSystemDepartmentUserStruct) error {

	db := model.Db

	//查询数据是否存在 不存在则新增
	if _, err := c.First(data.CorpId, data.Userid); errors.Is(err, gorm.ErrRecordNotFound) {
		if err := db.Table(c.TableName()).Create(&data).Error; err != nil {
			return err
		}
	}

	return nil
}

func (c *DingtalkSystemDepartmentUserStruct) List(where interface{}) ([]DingtalkSystemDepartmentUserStruct, error) {
	var dingtalkSystemDepartmentUserStruct []DingtalkSystemDepartmentUserStruct

	var db, err = model.BuildWhere(where)
	if err != nil {
		return nil, err
	}

	if err := db.Table(c.TableName()).Scan(&dingtalkSystemDepartmentUserStruct).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		return dingtalkSystemDepartmentUserStruct, nil
	}
	return dingtalkSystemDepartmentUserStruct, nil
}

func (c *DingtalkSystemDepartmentUserStruct) First(corpid, userId string) (DingtalkSystemDepartmentUserStruct, error) {
	var dingtalkSystemDepartmentUserStruct DingtalkSystemDepartmentUserStruct
	db := model.Db
	if err := db.
		Table(c.TableName()).
		Where("corp_id", corpid).
		Where("userid", userId).
		First(&dingtalkSystemDepartmentUserStruct).
		Error; err != nil {
		return dingtalkSystemDepartmentUserStruct, err
	}
	return dingtalkSystemDepartmentUserStruct, nil
}
